package cn.toys.modular.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import cn.toys.base.pojo.page.LayuiPageFactory;
import cn.toys.modular.entity.ProductAttr;
import cn.toys.modular.entity.ProductType;
import cn.toys.modular.service.IProductAttrService;
import cn.toys.modular.service.IProductTypeService;
import cn.toys.sys.core.exception.enums.BizExceptionEnum;
import cn.toys.sys.core.log.LogObjectHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 产品类型控制器
 *
 * @author guoyu
 * @Date 2021-02-27 14:23:24
 */
@Controller
@RequestMapping("/productAttr")
public class ProductAttrController extends BaseController {

    private String PREFIX = "/modular/productAttr/";

    @Autowired
    private IProductAttrService productAttrService;

    /**
     * 跳转到产品属性首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "productAttr.html";
    }

    /**
     * 跳转到添加产品属性
     */
    @RequestMapping("/productAttr_add")
    public String productAttrAdd() {
        return PREFIX + "productAttr_add.html";
    }

    /**
     * 跳转到修改产品属性
     */
    @RequestMapping("/productAttr_update")
    public String productAttrUpdate(@RequestParam Integer id) {
        ProductAttr productAttr = productAttrService.selectById(id);
        LogObjectHolder.me().set(productAttr);
        return PREFIX + "productAttr_edit.html";
    }

    /**
     * 获取产品类型列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name) {
        return LayuiPageFactory.createPageInfo(productAttrService.getCountByName(name), this.productAttrService.selectByName(name));
    }

    /**
     * 新增产品类型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ProductAttr productAttr) {
        productAttrService.insert(productAttr);
        return SUCCESS_TIP;
    }

    /**
     * 删除产品类型
     */
    @RequestMapping(value = "/remove")
    @ResponseBody
    public Object delete(@RequestParam Integer productAttrId) {
        productAttrService.deleteById(productAttrId);
        return SUCCESS_TIP;
    }

    /**
     * 修改产品类型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ProductAttr productAttr) {
        productAttrService.update(productAttr);
        return SUCCESS_TIP;
    }

    /**
     * 产品类型详情
     */
    @RequestMapping(value = "/detail/{productTypeId}")
    @ResponseBody
    public Object detail(@PathVariable("productTypeId") Integer productTypeId) {
        if (ToolUtil.isEmpty(productTypeId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        ProductAttr productAttr = productAttrService.selectById(productTypeId);
        Map<String, Object> roleMap = BeanUtil.beanToMap(productAttr);
        return ResponseData.success(roleMap);
    }
}
